package com.gitee.cashzhang.test.basic.thread;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/06/05 17:26
 */
public class VolatileTestMain {
  public static volatile boolean flag = true;

  public static void main(String[] args) throws InterruptedException {

    new Thread(new Runnable() {
      @Override
      public void run() {
        while (flag) {
        }
        System.out.println(Thread.currentThread().getName() + "线程停止，死循环被打开");
      }
    }).start();

    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        flag = false;
        System.out.println(Thread.currentThread().getName() + "修改 flag 为" + flag);
      }
    }).start();

    Thread.sleep(Integer.MAX_VALUE);
  }
}
